$(function () {

    //删除电子借款单的按钮触发
    $("#btn_xyz_del").click(function () {
        var pjRow = $('#tpfk_xyz_grid').edatagrid('getSelected');
        var pjIndex = $('#tpfk_xyz_grid').edatagrid('getRowIndex',pjRow);
        if(pjRow){
            $('#tpfk_xyz_grid').edatagrid('deleteRow',pjIndex);
//            countSumXyz();
        } else {
            $.messager.alert('提示','请选择要删除的信用证!');
        }
    });

    var fkId = $("#skf_fk_id").val();
    if(fkId==""){
        fkId = 0;
    }
    $("#tpfk_xyz_grid").datagrid({
        url:$.webPath() + "/zjgl/xyz/xyzList?pjsflxType=1&&sfkId="+fkId,
        fitColumns: true,
        singleSelect: true,
        width:730,
        height:200,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter:true,
        rownumbers: true,
        columns: [
            [
                {field:'id', title:'ID', hidden:true},
                {field:'xyzhm', title:'信用证号码', width:25,align: 'center'},
                {field:'yfczyh_qc', title:'出证行', width:30,align: 'center'},
                {field:'jzdw_jc', title:'交证单位', width:30,align: 'center'},
                {field:'dqje', title:'证面金额', width:20, formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                },align: 'right'},
                {field:'yll', title:'月利率(%)', width:20,
                    formatter:function(value){
                       return value+"%";
                    },align: 'right'},
                {field:'bzj', title:'保证金', width:25,
                    formatter:function(value){
                        return value;
                    }, formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                },align: 'right'},
                {field:'lxe', title:'利息额', width:20, formatter:function(value){
                    var sl = formatJe(value,",");
                    return sl;
                },align: 'right'},
                {field:'jkrq', title:'借款日期', width:20,align: 'center'},
                {field:'dqrq', title:'到期日', width:20,align: 'center'},
                {field:'bz', title:'备注', width:20,align: 'center'}
            ]
        ]
    })

    //搜索信用证
    $("#btn_xyz_search_add").click(function () {
        searcXyzwindow(tpfk_xyz_searchWindowOKbtnHandler);
    });
});


//查询销售单确定按钮回调函数
function tpfk_xyz_searchWindowOKbtnHandler(data) {
    $.each(data, function () {
        $("#tpfk_xyz_grid").edatagrid('appendRow', {
            'id': this.id,
            'xyzhm': this.xyzhm,
            'yfczyh_qc': this.yfczyh_qc,
            'jzdw': this.jzdw_jc,
            'dqje': this.dqje,
            'lxe':this.lxe,
            'bzj':this.bzj,
            'yll':this.yll,
            'jkrq': this.jkrq,
            'dqrq': this.dqrq,
            'bz': this.bz
        });
//        countSumPj();
    });
}

//计算计划总量
function countSumPj() {
    var sumdqje = 0;
    var data = $("#tpfk_pj_grid").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var dqje=parseFloat(data.rows[i]['dqje']);
        dqje = isNaN(dqje) ? 0 : dqje;
        sumdqje += dqje;
    }
    addFooter(sumdqje);
}

function addFooter(sumdqje) {
    var footer = null;
    footer = [{"spr_jc": "合计", "dqje": formatMoney(sumdqje)}];
    if (footer != null)
        $("#tpfk_pj_grid").edatagrid('reloadFooter',footer);
}
